<?php
/*
 *   This file is part of Verbum.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * 	 Copyright (c) 2008 Sergio Gabriel Teves <gabriel.sgt at gmail.com>
 */
 
require_once("entity.class.php");
 
class user extends entity {

	var $userid;
	var $password;
	var $mail;
	var $groupid;
	var $teamid;	
	var $timezone;
	var $lang;
	var $status;
	var $currentpwd;
	var $name;
	
	var $teamname;
	
	var $passexpire;
	var $passcode;
	
	var $options;

	var $lastlogin;
	
	public function __construct()
   {
   }

	// THIS CREATE A NEW INSTANCE OF THIS CLASS
	public function newInstance() {
		//$class = new ReflectionClass('user');	
		//return $class->newInstance();
		return new user;
	}

	// COMMON PUBLIC FUNCTIONS // NEEDED FOR LOADER
	public function get($disabled=false) {
		$sql = "SELECT * FROM ".USERS." WHERE userid = ".util::str(strtolower($this->userid));
		if (!$disabled) {
			$sql .= " AND status = ".util::str(STATUS_ACTIVE);
		}
		return $sql;
	}

	public function listAll() {
		global $STATUS_LIST;
		global $GROUP_LIST;
		
		//$status = util::arrayToString($STATUS_LIST,",",true);
		//$stln = getTranslatedList($STATUS_LIST);

		//$groupl = util::arrayToString($GROUP_LIST,",",true);
		//$groupln = getTranslatedList($GROUP_LIST);
		/*
		$sql = "SELECT u.*,t.name as teamname,ELT(FIELD(u.status,".$status."),".$stln.") as statustext, ELT(FIELD(u.groupid,".$groupl."),".$groupln.") as grouptext FROM ".USERS." u INNER JOIN ".TEAMS." t ON " .
				"u.teamid = t.id WHERE userid <> ".$this->userid." ORDER BY userid";
		*/
		$sql = "SELECT u.*,t.name as teamname FROM ".USERS." u INNER JOIN ".TEAMS." t ON " .
				"u.teamid = t.id WHERE userid <> ".util::str(strtolower($this->userid))." ORDER BY userid";
		return $sql;
/*
   		array_push($STATUS_LIST,$def->value);
   } elseif (substr_count($def->name,'GROUP_')>0) {
   		array_push($GROUP_LIST,$def->value);
*/


//SELECT u.*,ELT(FIELD(u.status,'ACTIVE','INACTIVE'), 'activo','inactivo') from users u		
	}

	public function listByStatus() {
		$sql = "SELECT * FROM ".USERS." WHERE status = ".util::str($this->status)." ORDER BY userid";
		return $sql;
	}

	public function listByFilters($having = null, $nothaving = null, $join = false) {
		global $STATUS_LIST;
		global $GROUP_LIST;
		
		//$status = util::arrayToString($STATUS_LIST,",",true);
		//$stln = getTranslatedList($STATUS_LIST);

		//$groupl = util::arrayToString($GROUP_LIST,",",true);
		//$groupln = getTranslatedList($GROUP_LIST);
		/*
		$sql = "SELECT u.*,t.name as teamname,ELT(FIELD(u.status,".$status."),".$stln.") as statustext, ELT(FIELD(u.groupid,".$groupl."),".$groupln.") as grouptext FROM ".USERS." u INNER JOIN ".TEAMS." t ON " .
			"u.teamid = t.id ";*/
		if ($join) {
			$sql = "SELECT u.*,t.name as teamname FROM ".USERS." u INNER JOIN ".TEAMS." t ON " .
					"u.teamid = t.id";			
		} else {
			$sql = "SELECT u.* FROM ".USERS." u ";			
		}

		$filter = "";
		if ($this->status != null) {
			$filter .= " u.status = ".util::str($this->status);
		}
		if ($this->teamid != null) {
			if ($filter != "") $filter .= " AND ";			
			$filter .= " u.teamid = ".util::str($this->teamid);
		}
		if ($this->groupid != null) {
			if (count($this->groupid)>1) {
				if ($filter != "") $filter .= " AND ";
				$filter .= " (u.groupid = ".util::str($this->groupid[0]);
				for ($i=1;$i<count($this->groupid);$i++) {
					$filter .= " OR u.groupid = ".util::str($this->groupid[$i]);
				}
				$filter.=")";
			} else {
				if ($filter != "") $filter .= " AND ";
				$filter .= " u.groupid = ".util::str($this->groupid);
			}
		}
		if ($this->userid != null) {
			if ($filter != "") $filter .= " AND ";			
			$filter .= " u.userid <> ".util::str(strtolower($this->userid));
		}
		if ($having!=null) {
			if ($filter != "") $filter .= " AND ";
			$filter .= " EXISTS (SELECT p.userid FROM ".PREFERENCES." p WHERE p.userid = u.userid AND ".addMFilter($having,"id").")";
		}
		if ($nothaving!=null) {
			if ($filter != "") $filter .= " AND ";
			$filter .= " NOT EXISTS (SELECT p.userid FROM ".PREFERENCES." p WHERE p.userid = u.userid AND ".addMFilter($nothaving,"id").")";
		}
				
		if ($filter != "") $sql .= " WHERE " .$filter;
		$sql .=" ORDER BY userid";
		return $sql;
	}

	public function simpleListByFilters() {
		$sql = "SELECT * FROM ".USERS." u";
		$filter = "";
		if ($this->status != null) {
			$filter .= " u.status = ".util::str($this->status);
		}
		if ($this->teamid != null) {
			if ($filter != "") $filter .= " AND ";			
			$filter .= " u.teamid = ".util::str($this->teamid);
		}
		if ($this->groupid != null) {
			if (count($this->groupid)>1) {
				if ($filter != "") $filter .= " AND ";
				$filter .= " (u.groupid = ".util::str($this->groupid[0]);
				for ($i=1;$i<count($this->groupid);$i++) {
					$filter .= " OR u.groupid = ".util::str($this->groupid[$i]);
				}
				$filter.=")";
			} else {
				if ($filter != "") $filter .= " AND ";
				$filter .= " u.groupid = ".util::str($this->groupid);
			}
		}

		if ($filter != "") $sql .= " WHERE " .$filter;
		$sql .=" ORDER BY userid";
		return $sql;
	}
		
	public function listByGroup() {
		$sql = "SELECT * FROM ".USERS." WHERE groupid = ".util::str($this->groupid)." ORDER BY userid";
		return $sql;
	}	
		
	public function insert() {
		$sql = "INSERT INTO ".USERS." (userid, mail, password, groupid, timezone, lang, teamid, status,passcode,passexpire,name) ";
		$sql .= "VALUES (".util::str(strtolower($this->userid)).",".util::str($this->mail).",".util::str(util::encrypt($this->password)).",";
		$sql .= util::str($this->groupid).",".util::str($this->timezone).",".util::str($this->lang).",".util::str($this->teamid);
		$sql .= ",".util::str($this->status).",".util::str("").",".util::str($this->passexpire).",".util::str(util::encode($this->name)).")";
		return $sql;
	}

	public function update() {
//		$opfusion = "";
//		foreach ($this->options as $value) {
//			$opfusion .= $value;
//		}
		$sql = "UPDATE ".USERS;
		$sql .= " set mail = ".util::str($this->mail);
		$sql .= ", groupid = ".util::str($this->groupid).", timezone = ".util::str($this->timezone).", lang = ".util::str($this->lang);
		$sql .= ", status = ".util::str($this->status).", teamid = ".util::str($this->teamid);
		$sql .=", name = ".util::str(util::encode($this->name));
		//$sql .=", options = ".util::str($opfusion);

		if ($this->passcode!='') {
			$sql .=", passexpire = ".util::str($this->passexpire);
			$sql .=", passcode = ".util::str(util::encrypt($this->passcode));
		}
		if ($this->lastlogin!='') {
			$sql .=", lastlogin = ".util::str($this->lastlogin);	
		}
		if ($this->password!='') {
			$this->currentpwd=util::encrypt($this->password);
			$sql .=", password = ".util::str($this->currentpwd);
		}
		$sql .= " WHERE userid = ".util::str(strtolower($this->userid));
		return $sql;
	}

	public function updateLogin() {
		$this->lastlogin = util::now();
		$sql = "UPDATE ".USERS;
		$sql .= " set lastlogin = ".util::str($this->lastlogin);
		$sql .= " WHERE userid = ".util::str(strtolower($this->userid));
		return $sql;
	}
	
	public function delete() {
		$sql = "DELETE FROM ".USERS." where userid = ".util::str(strtolower($this->userid));
		return $sql;
	}

	public function validate($pwd,$p=null) {
		if ($p!=null) {
			return $p == util::encrypt($pwd);	
		}
		return $this->currentpwd == util::encrypt($pwd);
	}

	public function validateNoEncrypt($pwd) {
		return $this->currentpwd == $pwd;
	}
		
	public function fill($array) {
		$this->userid=$array['userid'];
/*		if ($array['name']==null || $array['name']=="") {
			$this->name=$this->userid;
		} else {*/
			$this->name=util::decode($array['name']);
//		}
		$this->mail=$array['mail'];
		$this->currentpwd=$array['password'];
		$this->groupid=$array['groupid'];
		$this->timezone=$array['timezone'];		
		$this->lang=strtolower($array['lang']);
		$this->status=$array['status'];
		$this->teamid=$array['teamid'];
		$this->currentpasscode=$array['passcode'];
		$this->passexpire=$array['passexpire'];
		$this->lastlogin=$array['lastlogin'];
		$this->teamname=$array['teamname'];
		$this->id=$this->userid;
	}

}

?>
